package com.chaos.framework.web.service;

import com.chaos.framework.web.dto.login.manage.JwtData;
import io.jsonwebtoken.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Jacky
 * @date 2020/1/8 18:41
 */
@Component
public class JwtService {
    @Value("${app.sign:chaos}")
    private String sign;

    public String createToken(String userMu) {
        long now = System.currentTimeMillis();
        long exp = now + 1000 * 60 * 60 * 2;
        JwtBuilder builder = Jwts.builder().setId("888").setSubject("Jacky")
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, sign)
                .setExpiration(new Date(exp))
                .claim("userMu", userMu);
        return builder.compact();
    }

    public JwtData getJwtData(String token) {
        Claims claims =
                Jwts.parser().setSigningKey(sign).parseClaimsJws(token).getBody();
        return new JwtData((String) claims.get("userMu"), claims.getExpiration());
    }

    public String getUserMuInJwtData(String token) {
        try {
            return getJwtData(token).getUserMu();
        } catch (ExpiredJwtException e) {
            return "";
        }
    }
}
